/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Dao.exceptions.NonexistentEntityException;
import Dao.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Logica.Colaborador;
import Logica.Hotel;
import java.util.ArrayList;
import java.util.Collection;
import Logica.Disciplina;
import Logica.Deportista;
import Logica.Evento;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author User
 */
public class EventoJpaController implements Serializable {

    public EventoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Evento evento) throws PreexistingEntityException, Exception {
        if (evento.getHotelCollection() == null) {
            evento.setHotelCollection(new ArrayList<Hotel>());
        }
        if (evento.getDisciplinaCollection() == null) {
            evento.setDisciplinaCollection(new ArrayList<Disciplina>());
        }
        if (evento.getDeportistaCollection() == null) {
            evento.setDeportistaCollection(new ArrayList<Deportista>());
        }
        if (evento.getColaboradorCollection() == null) {
            evento.setColaboradorCollection(new ArrayList<Colaborador>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Colaborador idDirector = evento.getIdDirector();
            if (idDirector != null) {
                idDirector = em.getReference(idDirector.getClass(), idDirector.getIdColaborador());
                evento.setIdDirector(idDirector);
            }
            Collection<Hotel> attachedHotelCollection = new ArrayList<Hotel>();
            for (Hotel hotelCollectionHotelToAttach : evento.getHotelCollection()) {
                hotelCollectionHotelToAttach = em.getReference(hotelCollectionHotelToAttach.getClass(), hotelCollectionHotelToAttach.getIdHotel());
                attachedHotelCollection.add(hotelCollectionHotelToAttach);
            }
            evento.setHotelCollection(attachedHotelCollection);
            Collection<Disciplina> attachedDisciplinaCollection = new ArrayList<Disciplina>();
            for (Disciplina disciplinaCollectionDisciplinaToAttach : evento.getDisciplinaCollection()) {
                disciplinaCollectionDisciplinaToAttach = em.getReference(disciplinaCollectionDisciplinaToAttach.getClass(), disciplinaCollectionDisciplinaToAttach.getIdDisciplina());
                attachedDisciplinaCollection.add(disciplinaCollectionDisciplinaToAttach);
            }
            evento.setDisciplinaCollection(attachedDisciplinaCollection);
            Collection<Deportista> attachedDeportistaCollection = new ArrayList<Deportista>();
            for (Deportista deportistaCollectionDeportistaToAttach : evento.getDeportistaCollection()) {
                deportistaCollectionDeportistaToAttach = em.getReference(deportistaCollectionDeportistaToAttach.getClass(), deportistaCollectionDeportistaToAttach.getIdDeportista());
                attachedDeportistaCollection.add(deportistaCollectionDeportistaToAttach);
            }
            evento.setDeportistaCollection(attachedDeportistaCollection);
            Collection<Colaborador> attachedColaboradorCollection = new ArrayList<Colaborador>();
            for (Colaborador colaboradorCollectionColaboradorToAttach : evento.getColaboradorCollection()) {
                colaboradorCollectionColaboradorToAttach = em.getReference(colaboradorCollectionColaboradorToAttach.getClass(), colaboradorCollectionColaboradorToAttach.getIdColaborador());
                attachedColaboradorCollection.add(colaboradorCollectionColaboradorToAttach);
            }
            evento.setColaboradorCollection(attachedColaboradorCollection);
            em.persist(evento);
            if (idDirector != null) {
                idDirector.getEventoCollection().add(evento);
                idDirector = em.merge(idDirector);
            }
            for (Hotel hotelCollectionHotel : evento.getHotelCollection()) {
                hotelCollectionHotel.getEventoCollection().add(evento);
                hotelCollectionHotel = em.merge(hotelCollectionHotel);
            }
            for (Disciplina disciplinaCollectionDisciplina : evento.getDisciplinaCollection()) {
                Evento oldIdEventoOfDisciplinaCollectionDisciplina = disciplinaCollectionDisciplina.getIdEvento();
                disciplinaCollectionDisciplina.setIdEvento(evento);
                disciplinaCollectionDisciplina = em.merge(disciplinaCollectionDisciplina);
                if (oldIdEventoOfDisciplinaCollectionDisciplina != null) {
                    oldIdEventoOfDisciplinaCollectionDisciplina.getDisciplinaCollection().remove(disciplinaCollectionDisciplina);
                    oldIdEventoOfDisciplinaCollectionDisciplina = em.merge(oldIdEventoOfDisciplinaCollectionDisciplina);
                }
            }
            for (Deportista deportistaCollectionDeportista : evento.getDeportistaCollection()) {
                Evento oldIdEventoOfDeportistaCollectionDeportista = deportistaCollectionDeportista.getIdEvento();
                deportistaCollectionDeportista.setIdEvento(evento);
                deportistaCollectionDeportista = em.merge(deportistaCollectionDeportista);
                if (oldIdEventoOfDeportistaCollectionDeportista != null) {
                    oldIdEventoOfDeportistaCollectionDeportista.getDeportistaCollection().remove(deportistaCollectionDeportista);
                    oldIdEventoOfDeportistaCollectionDeportista = em.merge(oldIdEventoOfDeportistaCollectionDeportista);
                }
            }
            for (Colaborador colaboradorCollectionColaborador : evento.getColaboradorCollection()) {
                Evento oldIdEventoOfColaboradorCollectionColaborador = colaboradorCollectionColaborador.getIdEvento();
                colaboradorCollectionColaborador.setIdEvento(evento);
                colaboradorCollectionColaborador = em.merge(colaboradorCollectionColaborador);
                if (oldIdEventoOfColaboradorCollectionColaborador != null) {
                    oldIdEventoOfColaboradorCollectionColaborador.getColaboradorCollection().remove(colaboradorCollectionColaborador);
                    oldIdEventoOfColaboradorCollectionColaborador = em.merge(oldIdEventoOfColaboradorCollectionColaborador);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findEvento(evento.getIdEvento()) != null) {
                throw new PreexistingEntityException("Evento " + evento + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Evento evento) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Evento persistentEvento = em.find(Evento.class, evento.getIdEvento());
            Colaborador idDirectorOld = persistentEvento.getIdDirector();
            Colaborador idDirectorNew = evento.getIdDirector();
            Collection<Hotel> hotelCollectionOld = persistentEvento.getHotelCollection();
            Collection<Hotel> hotelCollectionNew = evento.getHotelCollection();
            Collection<Disciplina> disciplinaCollectionOld = persistentEvento.getDisciplinaCollection();
            Collection<Disciplina> disciplinaCollectionNew = evento.getDisciplinaCollection();
            Collection<Deportista> deportistaCollectionOld = persistentEvento.getDeportistaCollection();
            Collection<Deportista> deportistaCollectionNew = evento.getDeportistaCollection();
            Collection<Colaborador> colaboradorCollectionOld = persistentEvento.getColaboradorCollection();
            Collection<Colaborador> colaboradorCollectionNew = evento.getColaboradorCollection();
            if (idDirectorNew != null) {
                idDirectorNew = em.getReference(idDirectorNew.getClass(), idDirectorNew.getIdColaborador());
                evento.setIdDirector(idDirectorNew);
            }
            Collection<Hotel> attachedHotelCollectionNew = new ArrayList<Hotel>();
            for (Hotel hotelCollectionNewHotelToAttach : hotelCollectionNew) {
                hotelCollectionNewHotelToAttach = em.getReference(hotelCollectionNewHotelToAttach.getClass(), hotelCollectionNewHotelToAttach.getIdHotel());
                attachedHotelCollectionNew.add(hotelCollectionNewHotelToAttach);
            }
            hotelCollectionNew = attachedHotelCollectionNew;
            evento.setHotelCollection(hotelCollectionNew);
            Collection<Disciplina> attachedDisciplinaCollectionNew = new ArrayList<Disciplina>();
            for (Disciplina disciplinaCollectionNewDisciplinaToAttach : disciplinaCollectionNew) {
                disciplinaCollectionNewDisciplinaToAttach = em.getReference(disciplinaCollectionNewDisciplinaToAttach.getClass(), disciplinaCollectionNewDisciplinaToAttach.getIdDisciplina());
                attachedDisciplinaCollectionNew.add(disciplinaCollectionNewDisciplinaToAttach);
            }
            disciplinaCollectionNew = attachedDisciplinaCollectionNew;
            evento.setDisciplinaCollection(disciplinaCollectionNew);
            Collection<Deportista> attachedDeportistaCollectionNew = new ArrayList<Deportista>();
            for (Deportista deportistaCollectionNewDeportistaToAttach : deportistaCollectionNew) {
                deportistaCollectionNewDeportistaToAttach = em.getReference(deportistaCollectionNewDeportistaToAttach.getClass(), deportistaCollectionNewDeportistaToAttach.getIdDeportista());
                attachedDeportistaCollectionNew.add(deportistaCollectionNewDeportistaToAttach);
            }
            deportistaCollectionNew = attachedDeportistaCollectionNew;
            evento.setDeportistaCollection(deportistaCollectionNew);
            Collection<Colaborador> attachedColaboradorCollectionNew = new ArrayList<Colaborador>();
            for (Colaborador colaboradorCollectionNewColaboradorToAttach : colaboradorCollectionNew) {
                colaboradorCollectionNewColaboradorToAttach = em.getReference(colaboradorCollectionNewColaboradorToAttach.getClass(), colaboradorCollectionNewColaboradorToAttach.getIdColaborador());
                attachedColaboradorCollectionNew.add(colaboradorCollectionNewColaboradorToAttach);
            }
            colaboradorCollectionNew = attachedColaboradorCollectionNew;
            evento.setColaboradorCollection(colaboradorCollectionNew);
            evento = em.merge(evento);
            if (idDirectorOld != null && !idDirectorOld.equals(idDirectorNew)) {
                idDirectorOld.getEventoCollection().remove(evento);
                idDirectorOld = em.merge(idDirectorOld);
            }
            if (idDirectorNew != null && !idDirectorNew.equals(idDirectorOld)) {
                idDirectorNew.getEventoCollection().add(evento);
                idDirectorNew = em.merge(idDirectorNew);
            }
            for (Hotel hotelCollectionOldHotel : hotelCollectionOld) {
                if (!hotelCollectionNew.contains(hotelCollectionOldHotel)) {
                    hotelCollectionOldHotel.getEventoCollection().remove(evento);
                    hotelCollectionOldHotel = em.merge(hotelCollectionOldHotel);
                }
            }
            for (Hotel hotelCollectionNewHotel : hotelCollectionNew) {
                if (!hotelCollectionOld.contains(hotelCollectionNewHotel)) {
                    hotelCollectionNewHotel.getEventoCollection().add(evento);
                    hotelCollectionNewHotel = em.merge(hotelCollectionNewHotel);
                }
            }
            for (Disciplina disciplinaCollectionOldDisciplina : disciplinaCollectionOld) {
                if (!disciplinaCollectionNew.contains(disciplinaCollectionOldDisciplina)) {
                    disciplinaCollectionOldDisciplina.setIdEvento(null);
                    disciplinaCollectionOldDisciplina = em.merge(disciplinaCollectionOldDisciplina);
                }
            }
            for (Disciplina disciplinaCollectionNewDisciplina : disciplinaCollectionNew) {
                if (!disciplinaCollectionOld.contains(disciplinaCollectionNewDisciplina)) {
                    Evento oldIdEventoOfDisciplinaCollectionNewDisciplina = disciplinaCollectionNewDisciplina.getIdEvento();
                    disciplinaCollectionNewDisciplina.setIdEvento(evento);
                    disciplinaCollectionNewDisciplina = em.merge(disciplinaCollectionNewDisciplina);
                    if (oldIdEventoOfDisciplinaCollectionNewDisciplina != null && !oldIdEventoOfDisciplinaCollectionNewDisciplina.equals(evento)) {
                        oldIdEventoOfDisciplinaCollectionNewDisciplina.getDisciplinaCollection().remove(disciplinaCollectionNewDisciplina);
                        oldIdEventoOfDisciplinaCollectionNewDisciplina = em.merge(oldIdEventoOfDisciplinaCollectionNewDisciplina);
                    }
                }
            }
            for (Deportista deportistaCollectionOldDeportista : deportistaCollectionOld) {
                if (!deportistaCollectionNew.contains(deportistaCollectionOldDeportista)) {
                    deportistaCollectionOldDeportista.setIdEvento(null);
                    deportistaCollectionOldDeportista = em.merge(deportistaCollectionOldDeportista);
                }
            }
            for (Deportista deportistaCollectionNewDeportista : deportistaCollectionNew) {
                if (!deportistaCollectionOld.contains(deportistaCollectionNewDeportista)) {
                    Evento oldIdEventoOfDeportistaCollectionNewDeportista = deportistaCollectionNewDeportista.getIdEvento();
                    deportistaCollectionNewDeportista.setIdEvento(evento);
                    deportistaCollectionNewDeportista = em.merge(deportistaCollectionNewDeportista);
                    if (oldIdEventoOfDeportistaCollectionNewDeportista != null && !oldIdEventoOfDeportistaCollectionNewDeportista.equals(evento)) {
                        oldIdEventoOfDeportistaCollectionNewDeportista.getDeportistaCollection().remove(deportistaCollectionNewDeportista);
                        oldIdEventoOfDeportistaCollectionNewDeportista = em.merge(oldIdEventoOfDeportistaCollectionNewDeportista);
                    }
                }
            }
            for (Colaborador colaboradorCollectionOldColaborador : colaboradorCollectionOld) {
                if (!colaboradorCollectionNew.contains(colaboradorCollectionOldColaborador)) {
                    colaboradorCollectionOldColaborador.setIdEvento(null);
                    colaboradorCollectionOldColaborador = em.merge(colaboradorCollectionOldColaborador);
                }
            }
            for (Colaborador colaboradorCollectionNewColaborador : colaboradorCollectionNew) {
                if (!colaboradorCollectionOld.contains(colaboradorCollectionNewColaborador)) {
                    Evento oldIdEventoOfColaboradorCollectionNewColaborador = colaboradorCollectionNewColaborador.getIdEvento();
                    colaboradorCollectionNewColaborador.setIdEvento(evento);
                    colaboradorCollectionNewColaborador = em.merge(colaboradorCollectionNewColaborador);
                    if (oldIdEventoOfColaboradorCollectionNewColaborador != null && !oldIdEventoOfColaboradorCollectionNewColaborador.equals(evento)) {
                        oldIdEventoOfColaboradorCollectionNewColaborador.getColaboradorCollection().remove(colaboradorCollectionNewColaborador);
                        oldIdEventoOfColaboradorCollectionNewColaborador = em.merge(oldIdEventoOfColaboradorCollectionNewColaborador);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = evento.getIdEvento();
                if (findEvento(id) == null) {
                    throw new NonexistentEntityException("The evento with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Evento evento;
            try {
                evento = em.getReference(Evento.class, id);
                evento.getIdEvento();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The evento with id " + id + " no longer exists.", enfe);
            }
            Colaborador idDirector = evento.getIdDirector();
            if (idDirector != null) {
                idDirector.getEventoCollection().remove(evento);
                idDirector = em.merge(idDirector);
            }
            Collection<Hotel> hotelCollection = evento.getHotelCollection();
            for (Hotel hotelCollectionHotel : hotelCollection) {
                hotelCollectionHotel.getEventoCollection().remove(evento);
                hotelCollectionHotel = em.merge(hotelCollectionHotel);
            }
            Collection<Disciplina> disciplinaCollection = evento.getDisciplinaCollection();
            for (Disciplina disciplinaCollectionDisciplina : disciplinaCollection) {
                disciplinaCollectionDisciplina.setIdEvento(null);
                disciplinaCollectionDisciplina = em.merge(disciplinaCollectionDisciplina);
            }
            Collection<Deportista> deportistaCollection = evento.getDeportistaCollection();
            for (Deportista deportistaCollectionDeportista : deportistaCollection) {
                deportistaCollectionDeportista.setIdEvento(null);
                deportistaCollectionDeportista = em.merge(deportistaCollectionDeportista);
            }
            Collection<Colaborador> colaboradorCollection = evento.getColaboradorCollection();
            for (Colaborador colaboradorCollectionColaborador : colaboradorCollection) {
                colaboradorCollectionColaborador.setIdEvento(null);
                colaboradorCollectionColaborador = em.merge(colaboradorCollectionColaborador);
            }
            em.remove(evento);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Evento> findEventoEntities() {
        return findEventoEntities(true, -1, -1);
    }

    public List<Evento> findEventoEntities(int maxResults, int firstResult) {
        return findEventoEntities(false, maxResults, firstResult);
    }

    private List<Evento> findEventoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Evento.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Evento findEvento(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Evento.class, id);
        } finally {
            em.close();
        }
    }

    public int getEventoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Evento> rt = cq.from(Evento.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
